
    define(['init','ckeditor-jquery','fullcalendar_zh-cn','moment','userSelect','codeDef','formServ','bsMsgServ','dateUtil','uploadfiles'], 
    	function(init,ckeditor,calendarLang,moment,userSelect,codeDef,formServ,bsMsgServ,dateUtil,uploadfiles) {		
			
		'use strict';
			
		var rootid = 'message-page',
		
		
		//获取附件列表url
		uploadListurl = '/upload/query/getInitListDomain.action',
		
		bizcode = 'notification'
		
		//ckeditor对象
		var editor ;
		
		var btnEvent = {
			//用户选择
			userSelect:function(e){
				userSelect.openPage();
			}
			,
			//发布
			save:function(e){
				
				if(window.console){
					console.log('save fired...')
				}
				var _f = $('#'+rootid + ' .main-form')
				if(_f.length>0){
					var _data = formServ.getDataObj({form:_f});
					
					//判断id是否为临时变量
					var idVal = _data.id
					if(idVal && idVal.indexOf('TMP')>=0){
						_data.id = null;
					}
					//判断form必填项
					if(
						!formServ.validateForm({
							form:_f,
							rules:private_method.getFormRule()
						})
					){
						bsMsgServ.errorSlide('请填写表单内容');
						//释放按钮禁用状态
						$(e.currentTarget).removeClass('disabled');
						return
					}
					
					
					var usr = init.getCurrentUser();
					
					if(usr){
						if(!_data.public_user_id){
							_data.public_user_id = usr.id;						
							_data.public_user_name = usr.name;
						}
						
						if(!_data.public_dept_id){
							_data.public_dept_id = usr.deptid;
							_data.public_dept_name = usr.deptName;
						}
						
						if(!_data.typeId){
							_data.typeId = 16
							_data.typeName = '通知公告'
							_data.bizcode = bizcode
						}
						
						if(!_data.public_date){
							_data.public_date = dateUtil.getDateValue({format:'yyyy-MM-dd hh:mi:ss'});
						}
					}
					
					if(editor){
						_data.lob_contents = editor.getData();
					}
					
					var _d = $.toJSON(_data)
					var _param = {
						url:'/'+init.getSite()+'/docdetail/save.action',
						data:_d,
						type:'post',
						dataType:'json',
						contentType:'appliction/json;charset=utf-8',
						success:function(data){
							if(data){
								_f.form('load',data);
								
								if(data.lob_contents){
									if(editor){
										editor.setData(data.lob_contents);
									}
								}
								bsMsgServ.infoSlide(
									codeDef.getConfig({
										type:'operate',
										code:'success'
									})
								)
							}
							
							//释放按钮禁用状态
							$(e.currentTarget).removeClass('disabled');
						}
						,
						failure:function(){
							bsMsgServ.errorSlide(
								codeDef.getConfig({
									type:'operate',
									code:'error'
								})
							)
							//释放按钮禁用状态
							$(e.currentTarget).removeClass('disabled');
						}
					}
					$.ajax(_param);
				}
				
			}
			
			,
			cancel:function(){
				public_method.destory();
				init.loadContainerUrl('/getPage.action?code=pageMain');
			}
			,
			//附件
			archive:function(e){
				
			}
			,
			bind_btn_click:function(e){
				init.applyBtnEvt({btnEvt:e,btnConfig:btnEvent});
			}
			,
			response:function(_options){
				
			}
			
			,
			downloadFile:function(e){
				var _btn = $(e.currentTarget)
				
				var _id = _btn.attr('aid')
				
				var _url = '/'+init.getSite() +'/upload/downloadFileTest.action'
				var form = $("<form>");
				form.attr('style','display:none');
				form.attr('target','');
				form.attr('method','post');
				form.attr('action',_url);
				var input1 = $("<input>");
				input1.attr('type','hidden')
				input1.attr('name','id')
				input1.attr('value',_id);
				$('body').append(form);
				$(form).append(input1);
				
				form.submit();
				form.remove();
			}			
			,			
			removeFile:function(e){
				var _btn = $(e.currentTarget)
				
				var _id = _btn.attr('aid')
				
				if(_id){
					$.messager.confirm('警告', '确定执行删除操作?', function(r){
					
						if( r){	
							var _url = '/'+init.getSite() +'/upload/del.action';
							$.ajax({
								url:_url,
								data:{id:_id},
								success:function(data){
									if(data && data.status=='success'){
										private_method.initArchive();
										bsMsgServ.msgSlide();
									}else{
										bsMsgServ.warnSlide();
									}
								}
								,
								error:function(){
									bsMsgServ.warnSlide();
								}
							})
						}
					})
				}
				
			}
			,
			previewFile:function(e){
				var _btn = $(e.currentTarget)
				
				var _id = _btn.attr('aid')
				if(_id){
					var _url = '/'+init.getSite() +'/upload/previewFile.action';
					
					$.ajax({
						url:_url,
						data:{id:_id},
						success:function(data){
							var _title = $('#'+rootid + ' .modal-title')
							var _content = $('#'+rootid + ' .modal-body')
							
							if(data){
								
								if(data.archiveContent){
									//附件页面预览
									private_method.initArchiveContent(data)
								}else{
									//普通页面预览
									if(data.fileName){
										_title.text(data.fileName)
									}
									if(data.content){
										_content.append(data.content)	
									}
								}
							}
							var _m = $('#'+rootid + ' #doc-preview');
							_m.modal('show');
						}
						,
						error:function(){
							bsMsgServ.warnSlide();
						}
					})
				}
			}
			
			
			
		}
		
		var private_method = {
		
			getFormRule:function(){
				var rules = {
					rules:{
						name: 'required'
					}
					,
					messages:{
						name: '文档标题不允许为空'
					}
				}
				
				return rules;
			}
			,
			
			//获取附件上传参数
			getUploadParam:function(){
				//附件参数处理
				
				var _data = {
					bizcode: bizcode
				}
				//业务id处理
				var _idEl = $('#'+rootid +' #id')
				
				var _id
				if(_idEl.length ==1){
					
					
					var _tmpValue = $(_idEl).val()
					
					if(!_tmpValue || _tmpValue==''){
						//id为空,赋值临时id
						var _date = new Date();
						_id = 'TMP' + _date.getFullYear()+''+(_date.getMonth()+1)+''+_date.getDate()+'' +_date.getMilliseconds();
						var _tmpid = $('#'+rootid +' #tmpid')
						$(_tmpid).val(_id)
						$(_idEl).val(_id)
					}else{
						//id不为空，取id值
						_id = _tmpValue;
					}
				}
				
								
				_data.bizno=_id;	
				
				return _data;
				
			}
			,
			
			/**
			 * 初始化附件信息
			 * @param _options.bizcode	业务编码
			 * @param _options.bizno	业务id
			 * 
			 */
			initArchive:function(){
				
				var _el = $('#' + rootid + ' #archive');
				_el.empty();
				/**
				 * 根据审核状态判断是否显示按钮栏
				 */
				var showToolBar = true
				
										
				
				var _data = private_method.getUploadParam();
				
				var _param = {
					url:'/'+init.getSite()+uploadListurl,
					data:{condition:_data},
					type: 'post',
					success:function(data){
						if(window.console){
							console.log(' archive init ...')
						}
						var _html ='';
						if(data && data.rows){
							_html = '<ul>'
							$.each(data.rows,function(i,o){
								if(o.filename){
									_html += '<li><span>'+o.filename+'</span>'
									         
									var _name = o.filename.split('.');
									if(_name.length>0){
										var _extname = _name[_name.length-1];
										var _type = '';
										switch(_extname){
											case 'doc':
												_type = _extname;
												break;
											case 'docx':
												_type = _extname;
												break;
//												case 'xls':
//													_type = _extname;
//													break;
//												case 'xlsx':
//													_type = _extname;
//													break;
											default:
												
										}
									}
									
									if(_type){
										_html += '<a class="archive-preview archive-btn" btn-type="preview" aid="'+o.id+'" >'+'        预览' +'</span></a>';
										
									}
									
									_html += '<a class="archive-info archive-btn" btn-type="download" aid="'+o.id+'">'+'        下载'+'</a>' +
									'<a class="archive-remove archive-btn" btn-type="remove" aid="'+o.id+'"><span style="color:red">'+'        删除' +'</span></a>' +
										'</li>';	
								}
							})
							
							_html +='</ul>'
						}
						
						
						_el.append(_html);
						
						//绑定下载点击事件
						var file_btn = $('#'+rootid + ' .archive-info')
						if(file_btn.length>0){
							file_btn.unbind('click')
							file_btn.bind('click',	btnEvent.downloadFile)
						}
						
						//绑定预览点击事件
						var file_btn = $('#'+rootid + ' .archive-preview')
						if(file_btn.length>0){
							file_btn.unbind('click')
							file_btn.bind('click',	btnEvent.previewFile)
						}
						
						//绑定下载点击事件
						var file_btn = $('#'+rootid + ' .archive-remove')
						if(file_btn.length>0){
							file_btn.unbind('click')
							file_btn.bind('click',	btnEvent.removeFile)
						}
						
					}
				}
				
				$.ajax(_param);
			}
			
			,
			initUpload:function(){
				
				var _data = private_method.getUploadParam();
				
				//初始化上传附件组件
				uploadfiles.init({
					data:_data,							
					queue_id: rootid + ' #queue',
					file_id : rootid + ' #file_upload',
					rootPath: '/'+init.getSite(),
					rootid  : rootid,
					auto    : true,
					queueComplete : private_method.upload_success,
					uploadComplete: private_method.uploadSuccess
				});
				
			}
			
			,
			//附件页面预览
				initArchiveContent:function(data){
					if(window.console){
						console.log(' archiveContent loading ...')
					}
					if(data){
						
						if(data.typeName){
							//文档类型
							var contentHeaderEl = $('span.doctype')
							
							if(contentHeaderEl.length==1){
								$(contentHeaderEl).html('文档类别:'+data.typeName)
							}
						}
						
						//发布人员
						var public_dept_user
						var contentHeaderEl = $('span.public_dept_user')
					
						if(contentHeaderEl.length==1){
							if(data.public_dept_name){
								public_dept_user = '发布人: ' +data.public_dept_name
								if(data.public_user_name){
									public_dept_user += ' ' + data.public_user_name;
								}
							}else{
								if(data.public_user_name){
									public_dept_user = '发布人: ' +data.public_user_name
								}
							}
							if(public_dept_user)
							$(contentHeaderEl).html(public_dept_user)
						}
						
						if(data.public_date){										
											
							//发布时间										
							var contentEl = $('span.public_date')
							if(contentEl.length==1){
								$(contentEl).html(data.public_date)
							}
						}
						
						//附件内容
						var _p = {
							url	: '/portal/getContentArchive',
							data:{docid:data.id,bizcode:data.typeCode},
							success:function(_data){
								
								var _h = '';
								if(_data && _data.rows ){
									if((_data.rows)[0]){										
									
										var t = (_data.rows)[0]
										//文档内容										
										var contentEl = $('div.content-text')
										if(contentEl.length==1){
											_h = t.content;											
										}										
									}
									
									
									
									//文档附件
									var _el = $('.archive-container');
									var _html ='<h3>';								
									$.each(_data.rows,function(i,o){
										var _tmp = '附件: <a class="archive-info" aid="'+o.id+'">'+o.fileName+'</a>';
										
										_html = _html?_html+_tmp:_tmp;									
									})												
											
									
									_h = _html + '</h3>';
									
									
									var _content = $('#'+rootid + ' .modal-body')
									_content.html(_h)	
									var _m = $('#'+rootid + ' #doc-preview');
									_m.modal('show');
									
									//绑定点击事件
									var file_btn = $('#'+rootid  +' .archive-info')
									if(file_btn.length>0){
										file_btn.unbind('click')
										file_btn.bind('click',	private_method.downloadFile)
									}
								}
							}
							,
							error:function(){
								
								
							}						
						}
						$.ajax(_p);
					}
				}
				
				,
				
				upload_success:function(file,data){
					if(window.console)
						console.log(data);
						
					
					var _data = {
							bizcode:bizcode
						}
					
					var _id =$('#'+rootid + ' #id').val()
					if(_id){
							_data.bizno=_id	}
					
					private_method.initArchive(_data);
					bsMsgServ.msgSlide('文件上传成功')
				},
				
				uploadSuccess:function(file,data,response){
					
					
					var _data = {
							bizcode:bizcode
						}
					
					var _id =$('#'+rootid + ' #id').val()
					if(_id){
							_data.bizno=_id	}
					
					private_method.initArchive(_data);
					bsMsgServ.msgSlide('文件上传成功')
				}
				
		}
		
		
		var public_method= {
				
		
			//初始化页面
			initPage:function(){
				if(window.console){
					console.log('init message page...');
				}
				editor = init.setLobContent();
				
				//初始化附件组件
				private_method.initUpload();
				
				
				var _btn = $('#'+ rootid +' button.btn');
				
				if(_btn.length>0){
					_btn.unbind('click');
					_btn.bind('click',btnEvent.bind_btn_click);
				}
			},
			//销毁函数
			destory:function(){
				editor = null;
			}
		}
		
		return public_method;
    });
    
    

